<?php
declare (strict_types = 1);

namespace app\apiadmin\controller\choujiang;

use app\apiadmin\model\choujiang\ChouJiangModel;
use app\apiadmin\model\choujiang\PrizeShuZiRenUserModel;
use app\apiadmin\model\choujiang\PrizeUserModel;
use app\apiadmin\model\choujiang\ShuZiRenModel;
use app\apiadmin\model\system\UserModel;
use app\common\controller\BaseController;
use app\common\utils\Res;
use think\facade\Db;

class Lottery extends BaseController
{
    /**
     * 获取福袋抽奖信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function sendLotter()
    {
        $param = $this->request->param();
        $brid = $param['id'];
        if ($brid == ''){
            return Res::error("直播id为空");
        }
        $res_data = ChouJiangModel::where('brid', $brid)->where('status',1)->field('id,title,im_url,countdown,create_time,darw_state,pose_state,lott_state,status,publish')->find();
        return Res::ok($res_data);
    }
    /**
     * 获取参与用户的数据
     * @return \think\response\Json
     */
    public function getUserList()
    {
        $param = $this->request->param();
        $user_id = $param['id'];
        $brid = $param['brid'];
        $prid = $param['prid'];
        $tutorId = $param['tutorId'];
        if ($user_id == ''){
            return Res::error("用户ID为空");
        }
        $res_list =  Db::table('xh_siyu_prize_user_lottery')->insert(['user_id' => $user_id,'brid'=>$brid,'prid'=>$prid,'uid'=>$tutorId,'create_time'=>date('Y-m-d H:i:s',time())]);
        if ($res_list){
             return Res::ok('参与成功');
        }else{
             return Res::error('参与失败');
        }
    }
    /**
     * 发送中奖数据
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserLotteryList(){
        $param = $this->request->param();
        $brid = $param['brid'];//直播ID
        $id = $param['id']; //抽奖id
        $result=[];
        $prize_info = Db::name('siyu_prize')->where(['id'=>$id])->find();
        if($prize_info['status']!=3){
            return Res::ok($result);
        }
        switch($prize_info['pose_state']){
            case 0://未预设
                $where=['prid'=>$id,'brid'=>$brid,'state'=>1];
                $result = Db::name('siyu_prize_user_lottery')->where($where)->column('user_id');
                break;
            case 1://预设
                $where=['prid'=>$id,'status'=>1];
                $user = Db::name('siyu_prize_user')->where($where)->column('user_id');

                $where=['prid'=>$id,'status'=>1];
                $shuziren_user = Db::name('siyu_prize_shuziren_user')->where($where)->column('user_id');
                
                if($shuziren_user){
                    foreach($shuziren_user as &$item){
                        $item='szr_'.$item;
                    }
                }

                $result = array_merge($user,$shuziren_user);

                break;
            default:
                break;
        }
        return Res::ok($result);
    }
    /**
     * 显示中奖名单
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWinners()
    {
        $param = $this->request->param();
        $brid = $param['brid'];//直播ID
        $prid = $param['prid'];//抽奖ID

        $result=[];
        $pose_state=Db::name('siyu_prize')->where(['id'=>$prid])->value('pose_state');
        switch($pose_state){
            case 0://未预设
                $result=Db::name('siyu_prize_user_lottery')->alias('p')
                ->leftJoin('xh_siyu_user u','p.user_id=u.id')
                ->where(['p.brid'=>$brid,'p.prid'=>$prid,'p.state'=>1])
                ->field('p.user_id,u.nickname')
                ->select()
                ->toArray();
                break;
            case 1://预设
                $user_list = Db::name('siyu_prize_user')->alias('p')
                    ->leftJoin('xh_siyu_user u','p.user_id=u.id')
                    ->where(['p.prid'=>$prid,'p.status'=>1])
                    ->field('p.user_id,u.nickname')
                    ->select()
                    ->toArray();
                $shuziren_list = Db::name('siyu_prize_shuziren_user')->alias('p')
                    ->leftJoin('xh_siyu_shuziren_list l','p.user_id=l.id')
                    ->where(['p.prid'=>$prid,'p.status'=>1])
                    ->field('p.user_id,l.nick as nickname')
                    ->select()
                    ->toArray();
                $result=array_values(array_merge($user_list,$shuziren_list));
                break;
            default:
                break;
        }
        return Res::ok($result);
        
    }

    /**
     * 获取兑换奖项的用户信息
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function getLingjiang()
    {
        $param = $this->request->param();
        $user_id = $param['user_id'];
        $name = $param['name'];
        $phone = $param['phone'];
        $brid = $param['brid'];
        $res_list =  Db::table('xh_siyu_prize_user_lottery')->where('brid',$brid)->where('user_id',$user_id)->update(['name'=>$name,'phone'=>$phone,'update_time'=>date('Y-m-d H:i:s',time())]);
        if ($res_list){
            return Res::ok('获取成功');
        }else{
            return Res::error('获取失败');
        }
    }

}
